const db_promise = require("../config/db.config.js");

// Create and Save a new Tutorial
async function data_create(req, res) {
  
 
  // Validate request
  if (!req.body.title) {
    res.status(400).send({
      message: "Content can not be empty!"
    });
    return;
  }
   
  
  try {
     const [rows,fields] = await db_promise.query("select * from tutorials1 where title = ?",[req.body.title]);
  }catch(err){

    res.status(400).send({
      message: err
    });  
    return;
	}

    
  if (rows.length >= 1){
  	  
    res.status(400).send({
      message: req.body.title + " is exists!"
    });  	  
  	
  	
  }else{
  	
  	 const [rows, fields] = await db_promise.execute("insert into tutorials(title,description) values (?,?)",[req.body.title,req.body.description]);
  	 console.log(rows);
  	 
    res.status(400).send({
      message: req.body.title + " is ok!"
    });  	
  	
  }
  


      

};

module.exports = {
  data_create
};
